#include <SDL/SDL.h>
#include "BM_time.h"


/*******************************************************************************/
BMTimer::BMTimer() {
	startTicks = 0;
	pausedTicks = 0;
	paused = 0;
	started = 0;
}

void BMTimer::allocate(){
}

/*******************************************************************************/
void BMTimer::start(){
	paused = 0;
	started = 1;
	startTicks = SDL_GetTicks();
}

/*******************************************************************************/
void BMTimer::stop(){
    started = 0;
    paused = 0;
}
/*******************************************************************************/

void BMTimer::pause(){
    if( ( started == 1 ) && (paused == 0 ) ){
        paused = 1;
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}
/*******************************************************************************/
void BMTimer::resume(){
    if( paused == 1 ){
        paused = 0;
        startTicks = SDL_GetTicks() - pausedTicks;
        pausedTicks = 0;
    }
}

/*******************************************************************************/

int BMTimer::getTicks(){
    if( started == 1 ){
        if( paused == 1 ){
            return pausedTicks;
        }
        else{
            return SDL_GetTicks() - startTicks;
        }
    }
    return 0;
}
/*******************************************************************************/

int BMTimer::getStarted(){
    return started;
}

/*******************************************************************************/
int BMTimer::getPaused(){
    return paused;
}
/*******************************************************************************/

void BMTimer::freeTimer(){/*
		delete startTicks;
		delete pausedTicks;
		delete paused;
		delete started;
		delete fps;*/
}
/*******************************************************************************/
void BMTimer::frameDelay(){
	if( (signed int)(getTicks()) < (signed int)(1000 / fps) ){
		SDL_Delay( ( 1000 / fps ) - getTicks() );
	}
}
/*******************************************************************************/

void BMTimer::setFPS(int fpsr){
	fps = fpsr;
}
